package com.ciaojian.core.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ciaojian.core.model.DistRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.mapstruct.Mapper;

import java.util.Map;

/**
 * @author oxiny
 */
@Mapper
public interface DistRecordMapper extends BaseMapper<DistRecord> {
    /**
     * 获取分销员客数信息
     *
     * @param distId 分销员
     * @param page   分页插件
     * @return /
     */
    Page<Map<String, Object>> getCustomerByDistId(Page<Map<String, Object>> page, @Param("distId") Integer distId);

    /**
     * 获取佣金明细
     *
     * @param distId 分销员
     * @param page   分页插件
     * @return /
     */
    Page<Map<String, Object>> getDistRecordByDistId(Page<Map<String, Object>> page, @Param("distId") Integer distId);

    /**
     * 根据target_id查找记录
     *
     * @param targetId 被分享人
     * @return /
     */
    @Select("SELECT * FROM d_dist_record WHERE target_id = #{targetId} ")
    DistRecord getByTargetId(@Param("targetId") Integer targetId);

    /**
     * 统计总佣金
     *
     * @param distId 分销员
     * @return 总佣金
     */
    @Select("SELECT COALESCE(SUM(money), 0) FROM d_dist_record WHERE dist_id = #{distId} AND order_id IS NOT NULL")
    int sumMoneyByDistId(@Param("distId") Integer distId);

    /**
     * 统计分销客数
     *
     * @param distId 分销员
     * @return 分销客数
     */
    @Select("SELECT COALESCE(count(id),0) FROM d_dist_record WHERE dist_id = #{distId} AND order_id IS NOT NULL")
    int countSuccessByDistId(@Param("distId") Integer distId);

    /**
     * 统计子级订单数量
     *
     * @param distId 分销员
     * @return /
     */
    int countSubOrderNumByDistId(@Param("distId") Integer distId);

    Integer getOrderRecord(@Param("distId") Integer distId);
}
